<?php
class Term extends AppModel {

	var $name = 'Term';
    var $primaryKey = 'term_id';

    /**
     * Updating incoming referal
     * usage in Post Controller -> function index() , view()
     */
    function saveTerms($name,$website){
        $this->recursive = -1;
        $check = $this->checkByNameAndWebsite($name,$website);
        
        if(!empty($check)){
            $this->recursive = -1;
            $this->updateAll(array('Term.count'=>"Term.count + 1"),array('Term.term_id'=>$check['Term']['term_id']));
        }else{
            $this->create();
            $this->data =array('name'=>$name,'website'=>$website);
            $this->save($this->data);
        }
    }

    /**
     * Checking if referal exist or not
     */
    function checkByNameAndWebsite($name,$website) {
        $this->recursive = -1;
        $result = $this->find('first',array(
            'conditions'=>array('AND'=>array('Term.name'=>$name,'Term.website'=>$website)),
            'fields'=>array('Term.term_id')
        ));

        return $result;
    }

}
?>
